#pragma once

#include "../comm/util.hpp"
#include "../comm/log.hpp"

#include <iostream>
#include <string>
#include <unordered_map>
#include <cassert>
#include <vector>
#include <fstream>

#include "include/mysql.h"

namespace ns_model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;

    struct Question
    {
        std::string number; // 题目编号
        std::string title;  // 题目标题
        std::string star;   // 难度
        int cpu_limit;      // 时间复杂度
        int mem_limit;      // 空间复杂度
        std::string desc;   // 题目描述字段
        std::string header; // 题目预设给用户的编辑器代码字段
        std::string tail;   // 题目的测试用例,需要和header拼接形成完整代码
    };

    const std::string oj_questions = "oj_questions";

    class Model
    {
    private:
        unordered_map<string, Question> questions;

    public:
        Model()
        {
        }
        bool QueryMysql(const std::string &sql, vector<Question> *out)
        {
            MYSQL &my=mysql_init(nullptr);

            if(nullptr == mysql_reql_connect())
            {
                LOG(FATAL) << "连接数据库失败!" << "\n";
                return false;
            }
            return true;   
        }
        bool GetAllQuestions(vector<Question> *out)
        {
            std::string sql = "select * from ";
            sql += oj_questions;
            return QueryMysql(sql, out);
        }
        bool GetOneQuestion(const std::string &number, Question *q)
        {
            bool res = true;
            std::string sql = "select * from ";
            sql += oj_questions;
            sql += " where number=";
            sql += number;
            vector<Question> result;
            if (QueryMysql(sql, &result))
            {
                if (result.size() == 1)
                {
                    *q = result[0];
                    res = true;
                }
            }
            return res;
        }
        ~Model()
        {
        }
    };
}